<template>
  <div class="search-box">
    <div class="row">
      <div class="left">
        <el-icon><Search /></el-icon>
        <span>筛选搜索</span>
      </div>
      <div class="right">
        <el-button>重置</el-button>
        <el-button type="primary" @click="queryFn">查询搜索</el-button>
      </div>
    </div>
    <div class="rows">
      <div class="row-left">
        <div>专题名称:</div>
        <div>
          <el-input
            v-model="queryParams.productName"
            placeholder="商品名称"
          ></el-input>
        </div>
      </div>
      <div class="row-right">
        <span>推荐状态:</span>
        <el-select
          v-model="value"
          class="m-2"
          @change="selectFn"
          placeholder="Select"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>
    </div>
  </div>
  <div class="add-area">
    <div class="add-left">
      <el-icon>
        <Tickets />
      </el-icon>
      <span>数据列表</span>
    </div>
    <div class="add-right">
      <el-button plain @click="myShowFn">选择专题</el-button>
    </div>
  </div>
  <el-table
    :data="userList"
    border
    table-layout="auto"
    cell-class-name="align-c"
    @selection-change="handleSelectionChange"
    @select="select"
    @selectAll="selectAll"
  >
    <el-table-column align="center" type="selection"> </el-table-column>
    <el-table-column label="编号" prop="id" align="center" />
    <el-table-column label="专题名称" prop="subjectName" align="center" />
    <el-table-column label="是否推荐" prop="recommendStatus" align="center">
      <template #default="{ row }">
        <el-switch
          v-model="row.recommendStatus"
          :active-value="1"
          :inactive-value="0"
          @change="switchStatus(row)"
        />
      </template>
    </el-table-column>
    <el-table-column label="排序" prop="sort" align="center" />
    <el-table-column label="状态" align="center">
      <template #default="{ row }">
        <span v-if="row.recommendStatus === 1">推荐中</span>
        <span v-else>未推荐</span>
      </template>
    </el-table-column>
    <el-table-column label="操作" align="center">
      <template #default="{ row }">
        <el-button
          text
          type="primary"
          @click="allocRoleHandle(row.id, row.sort)"
          v-permission="'allocRole'"
          >设置排序</el-button
        >
        <el-button
          text
          type="primary"
          @click="deleteFn(row.id)"
          v-permission="'allocRole'"
          >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>

  <ElConfigProvider :locale="zhCn">
    <div class="box">
      <div class="select-box">
        <div class="select-box-left">
          <el-select v-model="value1" class="m-2" placeholder="全部">
            <el-option
              v-for="item in options1"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </div>
        <div>
          <el-button @click="selectionFn" type="primary">确定</el-button>
        </div>
      </div>
      <div class="page">
        <el-pagination
          v-model:current-page="Params.pageNum"
          v-model:page-size="Params.pageSize"
          :page-sizes="[5, 10, 15]"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="queryUserList"
          @current-change="queryUserList"
        />
      </div>
    </div>
  </ElConfigProvider>

  <ZhuanTiDity
    :myShow="myShow"
    :product="ProductSnss"
    :listData="myList"
    @myShowFn="myShowFn"
    @myHideFn="myHideFn"
  />
  <ZhuanTi
    :show="showAllocRole"
    :id="allocId"
    :sort="allocSort"
    @hide="hideAlloc"
    @fn="allocSortFn"
  />
</template>
<script setup lang="ts">
import { ref, reactive, provide } from "vue";
import { Search } from "@element-plus/icons-vue";
import { ElConfigProvider, ElMessage, ElMessageBox } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import ZhuanTi from "@/views/sms/components/Zhuantipaixu.vue";
import {
  SubjectStatusApi,
  recommendSubjectSearchApi,
  SubjectDeleteApi,
  recommendSubjectStatusAllApi,
  recommendSubjectApi,
} from "./api/index";
import ZhuanTiDity from "./components/ZhuanTiDity.vue";
import { Tickets } from "@element-plus/icons-vue";
const allocId = ref(0);
const allocSort = ref<number>(0);
const showAllocRole = ref(false);
const myId = ref([]);
const select = (row: any) => {
  // console.log("row:", row);
  myId.value = row.map((item: any) => item.id);
  //   row.forEach((item: any) =>{
  //     myStatus.value=item.status
  //   })
};
const selectAll = (selection: any) => {
  // console.log("selectAll:", selection);
  myId.value = selection.map((item: any) => item.id);
  //   selection.forEach((item: any) =>{
  //     myStatus.value=item.status
  //   })
};

// 选中checkbox事件
const myStatus = ref(1);

const selectionFn = () => {
  ElMessageBox.confirm("是否要修改推荐状态?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      // console.log(value1.value);
      if (value1.value === 2) {
        SubjectDeleteApi(myId.value.join(",") as any).then((res) => {});
        queryUserList();
      } else {
        SubjectStatusApi(myId.value.join(",") as any, value1.value);
        queryUserList();
      }
    })
    .catch(() => {
      ElMessage({
        type: "success",
        message: "取消成功",
      });
      queryUserList();
    });
};

const allocSortFn = (val: number) => {
  allocSort.value = val;
};

provide("allocSort", allocSort);

// 设置排序
const allocRoleHandle = (id: number, sort: number) => {
  showAllocRole.value = true;
  allocId.value = id;
  allocSort.value = sort;
  //   allocSort.value=0
};

// 删除
const deleteFn = (ids: number) => {
  ElMessageBox.confirm("是否要删除该推荐?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    SubjectDeleteApi(ids).then((res) => {
      if (res.code === 200) {
        // console.log(res.data);
        queryUserList();
      }
    });
    ElMessage({
      type: "success",
      message: "删除成功",
    });
  });
};

const hideAlloc = () => {
  showAllocRole.value = false;
  queryUserList();
  allocSort.value = 0;
};

const switchStatus = (row: SmsList) => {
  SubjectStatusApi(row.id, row.recommendStatus)
    .then((res) => {
      if (res.code === 200) {
        ElMessage.success("修改成功");
      } else {
        row.recommendStatus = row.recommendStatus === 1 ? 0 : 1;
      }
    })
    .catch(() => {
      row.recommendStatus = row.recommendStatus === 1 ? 0 : 1;
    });
};
// switchStatus()
// 专题推荐商品搜索
const queryParams = reactive<SearchList>({
  pageNum: 1,
  pageSize: 5,
  productName: "",
  recommendStatus: 1,
});
const userList = ref<any>([]);
const total = ref(0);
// 获取下拉框的值
const selectFn = (val: number) => {
  // console.log(val);
  queryParams.recommendStatus = val;
};
const queryFn = () => {
  recommendSubjectSearchApi(
    queryParams.pageNum,
    queryParams.pageSize,
    queryParams.productName,
    queryParams.recommendStatus
  ).then((res) => {
    if (res.code === 200) {
      // console.log(res.data.list);
      userList.value = res.data.list;
      total.value = res.data.total;
    }
  });
};

const queryUserList = () => {
  recommendSubjectApi(Params.pageNum, Params.pageSize).then((res) => {
    if (res.code === 200) {
      userList.value = res.data.list;
      total.value = res.data.total;

      res.data.list.forEach((item) => {
        allocSort.value = item.sort;
      });
    }
  });
};
const Params = reactive<smsData>({
  pageNum: 1,
  pageSize: 5,
  keyword: "",
});

// 请求一开始的默认数据
queryUserList();

// 全选
const multipleSelection = ref<SmsList[]>([]);
const handleSelectionChange = (val: SmsList[]) => {
  multipleSelection.value = val;
  //   console.log(123123);
};
// 下拉框
const value = ref("全部");

const options = ref([
  {
    value: 0,
    label: "未推荐",
  },
  {
    value: 1,
    label: "推荐中",
  },
]);
const value1 = ref();

const options1 = ref([
  {
    value: 1,
    label: "设为推荐",
  },
  {
    value: 0,
    label: "取消推荐",
  },
  {
    value: 2,
    label: "删除",
  },
]);

// 控制选择商品弹框显示
let myShow = ref<boolean>(false);
const ProductSnss = ref("");

// 显示
let myList = ref<ProductList>();
const myShowFn = () => {
  // 点击商品请求数据

  myShow.value = true;
};

// 注入
provide("myList", myList);
// 隐藏
const myHideFn = () => {
  myShow.value = false;
};
</script>
<style lang="less" scoped>
:deep(.align-c) {
  .cell {
    text-align: center !important;
  }
}
.search-box {
  box-shadow: 0 0 4px 2px #ccc;
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 20px;
  .row {
    padding: 20px 0;
    display: flex;
    &:first-child {
      justify-content: space-between;
    }

    &:last-child {
      width: 400px;
      span {
        width: 100px;
      }
    }
  }
  .rows {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 800px;
    .row-left {
      display: flex;
      // width: ;
      justify-content: space-between;
      align-items: center;
      // margin-right: 80px;
    }
    .row-right {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
  }
}
.box {
  display: flex;
  justify-content: space-between;
  align-items: center;
  .select-box {
    display: flex;
    justify-content: space-between;
    .select-box-left {
      margin-right: 20px;
    }
  }
  .page {
    padding: 20px;
    display: flex;
    justify-content: flex-end;
  }
}

.add-area {
  box-shadow: 0 0 4px #ccc;
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
